package sing.cloud.alibaba.seata.service.impl;

import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sing.cloud.alibaba.seata.mapper.IAccountMapper;
import sing.cloud.alibaba.seata.service.IAccountService;

import java.math.BigDecimal;

/**
 * @author xing
 * @date 2022/9/3 18:48
 */
@Service("accountService")
@Slf4j
public class AccountServiceImpl implements IAccountService {

    @Autowired
    private IAccountMapper accountMapper;

    @Override
//    @GlobalTransactional
    public void decrease(Long userId, BigDecimal money) {
        log.info("------->account-service中扣减账户余额开始");
        //模拟超时异常，全局事务回滚
        //暂停几秒钟线程
        //try { TimeUnit.SECONDS.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); }
        accountMapper.decrease(userId,money);
        log.info("------->account-service中扣减账户余额结束");
    }
}
